package artibella.media.video 
{
	import artibella.media.ADisplay;
	import artibella.media.DisplayMode;
	import flash.display.StageDisplayState;
	import flash.media.Video;
	
	/**
	 * ...
	 * @author pbordachar
	 */
	
	public class VideoDisplay extends ADisplay 
	{
		private var _screen:Video;
		private var _screenRatio:Number;
		
		public function VideoDisplay( w:int = 320, h:int = 240, bgColor:int = -1, dm:String = DisplayMode.FIT  ) 
		{
			_screen = new Video( w, h );
			_screen.smoothing = true;
			addChild( _screen );
			
			super( w, h, bgColor, dm );
		}
		
		public function get screen():Video
		{
			return _screen;
		}
		
		// - - -
		
		public function setScreenRatio( r:Number ):void
		{
			_screenRatio = r;
		}
		
		private function _resizeScreen( w:int, h:int ):void
		{
			_ratio = w / h;
			
			switch( _displayMode )
			{
				case DisplayMode.DISTORT:
				
					_screen.width = w;
					_screen.height = h;
					
					break;
				
				case DisplayMode.FIT:
				
					if ( _screenRatio > _ratio )
					{
						_screen.height = h;
						_screen.width = _screen.height * _screenRatio;
					} else {
						
						_screen.width = w;
						_screen.height = _screen.width / _screenRatio;
					}
				
					break;
					
				case DisplayMode.FULL:
				
					if ( _screenRatio > _ratio )
					{
						_screen.width = w;
						_screen.height = _screen.width / _screenRatio;
						
					} else {
						
						_screen.height = h;
						_screen.width = _screen.height * _screenRatio;
					}
				
					break;
					
				case DisplayMode.NORMAL:
				
					// keud
			}
			
		}
		
		// - - - IStageObserver
		
		override public function stageUpdate( w:int, h:int ):void 
		{
			super.stageUpdate( w, h );
			
			if ( _screen != null ) {
			
				switch ( _displayState )
				{
					case StageDisplayState.FULL_SCREEN :

						_resizeScreen( w, h );
						
						break;
						
					case StageDisplayState.NORMAL :
					
						_resizeScreen( _width, _height );
						
				}
			}
		}
		
		// - - - IDisposable
		
		override public function dispose():void
		{
			super.dispose();
		}
	}
}